package cn.cyq.readerhelper.ocr.api;

import cn.cyq.readerhelper.common.BaiSpeechConf;
import cn.cyq.readerhelper.common.JsonResult;
import cn.cyq.readerhelper.ocrjdk.MyAipOcr;
import com.baidu.aip.ocr.AipOcr;
import com.baidu.aip.speech.AipSpeech;
import com.baidu.aip.speech.TtsResponse;
import com.baidu.aip.util.Util;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("ocr/baidu")
public class BaiduOcrApi {


    @Autowired
    private MyAipOcr aipOcr;

    @Autowired
    private AipSpeech aipSpeech;

    @Autowired
    private BaiSpeechConf baiSpeechConf;

    @RequestMapping("getContent")
    public JsonResult getContent(@RequestParam("file") MultipartFile file, String language_type) {
        byte[] bytes;
        try {
            // 将文件保存到服务器中
            bytes = file.getBytes();
            System.out.println("file size : " + (int) (bytes.length) / 1024 + "kb");
        } catch (IOException e) {
            e.printStackTrace();
            return new JsonResult(500, null, "上传失败");
        }

        HashMap<String, String> options = new HashMap<>();
        if (language_type != null && !"".equals(language_type)) {
            options.put("language_type", language_type);
        }
        options.put("paragraph", "false");
        options.put("probability", "false");
        //JSONObject jsonObject = aipOcr.basicAccurateGeneral("C:\\Users\\Administrator\\Desktop\\1.png", options);
        JSONObject jsonObject = aipOcr.basicAccurateGeneral(bytes, options);
        Map<String, Object> data = jsonObject.toMap();
        StringBuilder stringBuilder = new StringBuilder();
        if (data.containsKey("words_result_num")) {
            int words_result_num = (int) data.get("words_result_num");
            if (words_result_num > 0) {
                List<Map<String, String>> words_result = (List<Map<String, String>>) data.get("words_result");
                for (Map<String, String> stringMap : words_result) {
                    stringBuilder.append(stringMap.get("words"));
                }
            }
        }
        return new JsonResult(0, stringBuilder.toString(), "ok");

    }


    @RequestMapping("getVoice")
    public JsonResult getVoice(String text, Integer per, Integer pit) {
        if ("".equals(text)) {
            return new JsonResult(500, null, "参数错误");
        }
        HashMap<String, Object> options = new HashMap<>();
        //度小宇=1，度小美=0，度逍遥（基础）=3，度丫丫=4
        //度逍遥（精品）=5003，度小鹿=5118，度博文=106，度小童=110，度小萌=111，度米朵=103，度小娇=5
        if (per == null) {
            per = 103;
        }

        if (pit == null) {
            pit = 3;
        }
        options.put("per", per);//声音
        options.put("spd", 3);//语速
        options.put("pit", pit);//音调

        if (text.length() < 360) {
            TtsResponse ttsResponse = aipSpeech.synthesis(text, "zh", 1, options);
            byte[] data = ttsResponse.getData();
            if (data != null) {
                try {
                    String fileName = System.currentTimeMillis() + ".mp3";
                    Util.writeBytesToFileSystem(data, baiSpeechConf.getVideofilepath() + "\\" + fileName);
                    return new JsonResult(0, "http://3454f485l2.yicp.fun/files/" + fileName, "ok");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
        return new JsonResult(500, "", "文字过长");


    }

}
